<?php
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
use Guzzle\Plugin\Cookie\CookiePlugin;
use TbfHttp\Ajax;

class TbfTestCase extends \PHPUnit_Framework_TestCase{
    public static function assertHtml($string,$msg=''){
        self::ok(self::isHtml($string),
            sprintf("not html\n%s\n%s",$string,$msg)
        );
    }
    public static function isHtml($string){
        return TbfString::Contains($string,'<') && !TbfString::Contains($string,'系统发生错误');
    }
    public static function assertEquals($expected,$actual,$message = '',$delta = 0,
                                        $maxDepth = 10,$canonicalize = FALSE,$ignoreCase = FALSE){
        if ((is_numeric($expected) && is_numeric($actual)) ||
            is_array($expected) && is_array($actual) ||
            is_object($expected) && is_object($actual)){
            parent::assertEquals($expected,$actual,$message,$delta,
                $maxDepth,$canonicalize,$ignoreCase);

            return;
        }
        parent::assertSame($expected,$actual,$message,$delta,$maxDepth,
            $canonicalize,$ignoreCase);

        return;
    }
    public static function ok($condition,$message = ''){
        parent::assertTrue($condition,$message);
    }
    //修正assertContains的奇葩顺序
    public static function assertStringContains($s,$substr,$message = ''){
        parent::assertContains($substr,$s,$message);
    }
    //修正assertNotContains的奇葩顺序
    public static function assertStringNotContains($s,$substr,$message = ''){
        parent::assertNotContains($substr,$s,$message);
    }
    public static function getTestUrlBase(){
        return TbfConfig::$TestUrlBase;
    }

    /** @var  \TbfHttp\Ajax */
    protected static $ajax;
    /** @return \TbfHttp\Ajax */
    public static function getAjax(){
        if (!empty(self::$ajax)){
            return self::$ajax;
        }
        $client = new \Guzzle\Service\Client(static::getTestUrlBase(),array(
            'curl.options'=> array(
                'CURLOPT_PROXY'=>'',//不要用代理
                'CURLOPT_USERPWD'=>''//http普通密码  user:password
            )
        ));
        $cookie_plugin = new CookiePlugin(new ArrayCookieJar());
        $client->addSubscriber($cookie_plugin);
        $ajax = new Ajax($client);
        $ajax->testCode = TbfConfig::$TestCode;
        self::$ajax = $ajax;
        return $ajax;
    }
    public static function resetAjax(){
        self::$ajax = null;
    }

    /**
     * 文档请看 \TbfHttp\Ajax
     * @param $config
     * @return \Guzzle\Http\EntityBodyInterface|\Guzzle\Http\Message\Response|mixed|null|string
     */
    public static function ajax($config){
        return self::getAjax()->request($config);
    }


}
